package com.simple.quotes.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author Petro
 */
public class MainFrame extends JFrame{
    private JLabel status;
    private JTextArea textArea;

    public MainFrame() throws HeadlessException {
        setTitle("SimpleQuotes");

        // on close
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        addWindowListener(new ExitListener(this));

        // size & location
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenWidth = screenSize.width;
        int screenHeight = screenSize.height;
        setBounds(screenWidth/4, screenHeight/4, screenWidth/2, screenHeight/2);

        // layout manager
        setLayout(new BorderLayout());

        // menu
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu menuFile = new JMenu("File");
        menuFile.setMnemonic('F');
        menuBar.add(menuFile);

        menuFile.add(new LoadFileAction(this));

        menuFile.addSeparator();

        JMenuItem menuItemExit = menuFile.add("Exit");
        menuItemExit.setToolTipText("Exit program");
        menuItemExit.setMnemonic('x');
        menuItemExit.addActionListener(new ExitListener(this));

        JMenu menuAbout = new JMenu("Help");
        menuAbout.setMnemonic('H');
        menuBar.add(menuAbout);

        JMenuItem menuItemAbout = menuAbout.add("About");
        menuItemAbout.setMnemonic('A');
        menuItemAbout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(MainFrame.this, "SimpleQuotes by Petro", "About", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        // data
        textArea = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(textArea);
        add(scrollPane);

        // status bar
        status = new JLabel(" ");
        status.setBorder(BorderFactory.createEtchedBorder(Color.white, new Color(178, 178, 178)));
        status.setOpaque(true); //Делаем фон строки непрозрачным
        add(status, BorderLayout.SOUTH);
    }

    public JTextArea getTextArea() {
        return textArea;
    }

    public void setStatus(String text) {
        status.setText(text);
    }
}
